{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[338.   1.]\n",
      "[333.   1.]\n",
      "[328.   1.]\n",
      "[207.   1.]\n",
      "[226.   1.]\n",
      "[25.  1.]\n",
      "[179.   1.]\n",
      "[60.  1.]\n",
      "[208.   1.]\n",
      "[606.   1.]\n",
      "[[   2.67956187 -187.97002933]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "x_data = [ 338., 333., 328., 207., 226., 25., 179., 60., 208., 606.]\n",
    "y_data = [ 640., 663., 619., 393., 428., 27., 193., 66., 226., 1591.]\n",
    "\n",
    "n = 1\n",
    "X = np.ones((len(x_data),n + 1), dtype= np.float)\n",
    "Y = np.zeros((len(y_data)), dtype= np.float)\n",
    "\n",
    "\n",
    "row = 0\n",
    "for x in X:\n",
    "    x[0] = x_data[row]\n",
    "    #print(x)\n",
    "    row += 1\n",
    "#print(X)\n",
    "\n",
    "for i in range(0, len(y_data)):\n",
    "    Y[i] = y_data[i]\n",
    "    \n",
    "#求解析解\n",
    "Xt = np.transpose(X)\n",
    "X2 = np.matrix( np.matmul(Xt, X))\n",
    "XtXinv = X2.I\n",
    "XtXinvXt = np.matmul(XtXinv,Xt)\n",
    "W = np.matmul(XtXinvXt,Y)\n",
    "\n",
    "\n",
    "print(W)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
